package com.xiaodong.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodong.reggie.currency.R;
import com.xiaodong.reggie.entity.Category;
import com.xiaodong.reggie.entity.Dish;
import com.xiaodong.reggie.entity.Employee;
import com.xiaodong.reggie.service.CategoryService;
import com.xiaodong.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询分类数据
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize) {

        Page<Category> pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    //新型菜品添加
    @PostMapping
    public R<String> save(@RequestBody Category category) {

        categoryService.save(category);

        return R.success("新型菜品添加成功");

    }

    //修改菜单类型或顺序
    @PutMapping
    public R<String> update(@RequestBody Category category) {

        categoryService.updateById(category);

        return R.success("修改成功");

    }

    //删除菜品
    @DeleteMapping
    public R<String> delete(Long id) {

        log.info("id的值：={}", id);

        categoryService.remove(id);

        return R.success("删除成功");

    }

    //新增菜品
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

        if(category.getType()!=null){

            queryWrapper.eq(Category::getType, category.getType());

        }

        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> one = categoryService.list(queryWrapper);

        return R.success(one);

    }

}
